package com.hushao.aimi.controller;

import com.hushao.aimi.common.ServerResponse;
import com.hushao.aimi.vo.CategoryVo;
import com.hushao.aimi.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 分类控制器
 * @author: 胡少
 * @create: 2018-06-06 11:33
 **/
@Api(value = "分类操作接口")
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 添加分类
     * @param categoryVo
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "添加分类信息")
    @PostMapping
    public ServerResponse addCategory(@RequestBody CategoryVo categoryVo)throws Exception{
        return categoryService.addCategory(categoryVo);
    }

    /**
     * 修改品类
     * @param categoryVo
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "修改分类信息")
    @PutMapping
    public ServerResponse updateCategory(@RequestBody CategoryVo categoryVo)throws Exception{
        return categoryService.updateCategory(categoryVo);
    }


    @ApiOperation(value = "查询子节点的分类信息，不递归")
    @GetMapping("/{categoryId}")
    public ServerResponse getChildrenParallelCategory(@PathVariable("categoryId") int categoryId) throws Exception{
        return categoryService.getChildrenParallelCategory(categoryId);

    }

    @ApiOperation(value = "查询当前分类信息，递归查询子节点信息")
    @GetMapping("/getDeepCategory")
    public ServerResponse getCategoryAndDeepChildrenCategory(@RequestParam(value = "categoryId",defaultValue = "0") int categoryId) throws Exception{

        return categoryService.getCategoryAndChildrenById(categoryId);

    }


    /**
     * 获取品类列表（分页）
     * @param name
     * @param pageable
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取品类列表（分页）")
    @GetMapping
    public ServerResponse getCategoryByNameLike(@RequestParam(value = "name",required = false,defaultValue = "")String name,
                                                Pageable pageable)throws Exception{
        return categoryService.getCategoryByNameLike(name,pageable);
    }

    /**
     * 获取品类列表
     * @return
     */
    @ApiOperation(value = "获取品类列表")
    @GetMapping("/list")
    public ServerResponse listCategory(){
        return categoryService.listCategory();
    }

    /**
     * 删除品类
     * @return
     */
    @ApiOperation(value = "删除品类")
    @DeleteMapping("/{categoryId}")
    public ServerResponse delCategory(@PathVariable Integer categoryId)throws Exception{
        return categoryService.delCategory(categoryId);
    }
}

